Skip to content
This repository was archived by the owner on Oct 17, 2026. It is now read-only.

feat(usage): learn from the start of past queries - #4

Merged
oddurs merged 2 commits into
mainfrom
feat/learn-prefixes
Oct 16, 2026
Merged

oddurs merged 2 commits into
mainfrom
feat/learn-prefixes

Conversation

@oddurs

@oddurs oddurs commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Problem

Learned choices only applied to the exact query. Choosing Safari for "safari" didn't help the next "sa", so learning never let anyone type less.

Approach

Usage.choiceBoosts(_:) scans the stored choices once per query:

  • exact query: the full boost (120), unchanged;
  • a query that starts a stored one, or that a stored one starts: half the boost, scaled by the shared length over the longer length. "sa" after "safari" earns 20, "safar" after "saf" earns 36;
  • an item chosen for several queries keeps its best boost.

Ranking builds a Usage.Booster once per keystroke and looks each candidate up in it, instead of folding the query and checking choices per candidate.

Also in this PR: tie-breaks without ICU

The pre-push check failed on the debug performance gate ("s": 72 ms against 40 ms) while this machine sat at load average 48. Profiling the outlier showed one-letter queries tie hundreds of names, and each tie called localizedStandardCompare. Ties now compare the folded keys already computed for matching. Minimum of five interleaved release runs on the 2,000-name benchmark: 989 µs on main, 704 µs here; the debug gate went from 72 ms to 15 ms under the same load. It's a separate commit so it reads on its own.

Look at sceptically

  • The 1/2 and proportional scaling are judgement calls, tuned so an exact choice always beats a shared start.
  • One stored choice can now boost an item for many queries. It only applies to items that already match, so it reorders results but never adds them.
  • Folded-key ordering differs from localizedStandardCompare for digits ("Item 10" sorts before "Item 9") and some scripts. It only decides exact ties in score and length.
  • aChoiceForALongerQueryLeansAShorterOneTheSameWay was confirmed to fail on the old code (it picks Safari).

Checklist

  • scripts/task check passes locally
  • Ranking changes include tests showing the order they fix
  • UI changes include before/after screenshots (no visual change)

Learning only helped the exact query typed before, so choosing Safari for "safari" did nothing for "sa", which is the point of learning: typing less. A past choice now lends a share of its boost to queries that share its start, and the boosts for a query are worked out once instead of once per candidate.
A one-letter query ties hundreds of names, and localizedStandardCompare on each tie cost more than matching. The folded keys are already in memory and order the same way for ties. Minimum of five interleaved release runs on the 2,000-name benchmark: 989 µs before, 704 µs after.
@oddurs
oddurs merged commit 370d478 into main Oct 16, 2026
2 checks passed
@oddurs
oddurs deleted the feat/learn-prefixes branch October 16, 2026 23:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant